<template>
    <z-paging :use-virtual-list="true" :auto-show-back-to-top="true" :loading-full-fixed="true"
        :auto-show-system-loading="true" ref="pagingRef" @query="queryListFn" :auto="false">
        <template #cell="{ item, index }">
            <view class="cards mx-32 mt-30">
                <view class="df ac">
                    <uv-tags :text="item.StateText" plain size="mini" :type="item.StateType"></uv-tags>
                    <view class="card-title-name">{{ item.Name }}</view>
                    <view style="margin-left: auto;">{{ item.ObjectTypeCodeText }}</view>
                </view>
                <view class="card-content">{{ item.Content }}</view>
                <view class="card-title">巡查对象：{{ item.ObjectName }}</view>
                <view class="card-title">统一编号：{{ item.ObjectId }}</view>
                <view class="df jsa fs-24 mt-20 ffrw">
                    <uv-button size="mini" @click="gotoTaskPage(SYSFORMOPERTYPE.View, item.Id)">任务查看</uv-button>
                    <uv-button type="success" size="mini" @click="gotoTaskPage(SYSFORMOPERTYPE.Edit, item.Id)"
                        v-if="checkRight(item.State, SYSTASKOPERTYPE.Publish)">任务编辑</uv-button>
                    <uv-button type="primary" size="mini" @click="operTask(item.Id, SYSTASKOPERTYPE.Publish)"
                        v-if="checkRight(item.State, SYSTASKOPERTYPE.Publish)">发布</uv-button>
                    <uv-button type="warning" size="mini" @click="operTask(item.Id, SYSTASKOPERTYPE.CancelPublish)"
                        v-if="checkRight(item.State, SYSTASKOPERTYPE.CancelPublish)">取消发布</uv-button>
                    <uv-button type="error" size="mini" @click="operTask(item.Id, SYSTASKOPERTYPE.Delete)"
                        v-if="checkRight(item.State, SYSTASKOPERTYPE.Delete)">删除</uv-button>
                    <uv-button size="mini" @click="gotoReportPage(SYSFORMOPERTYPE.View, item.ReportId)"
                        v-if="checkRight(item.State, SYSTASKOPERTYPE.ReportView)">填报查看</uv-button>
                    <uv-button type="success" size="mini" @click="gotoReportPage(SYSFORMOPERTYPE.Edit, item.ReportId)"
                        v-if="checkRight(item.State, SYSTASKOPERTYPE.Submit)">巡查填报</uv-button>
                    <uv-button type="success" size="mini" @click="operTask(item.Id, SYSTASKOPERTYPE.Submit)"
                        v-if="checkRight(item.State, SYSTASKOPERTYPE.Submit)">提交</uv-button>
                    <uv-button type="warning" size="mini" @click="operTask(item.Id, SYSTASKOPERTYPE.Withdraw)"
                        v-if="checkRight(item.State, SYSTASKOPERTYPE.Withdraw)">撤回</uv-button>
                    <uv-button type="success" size="mini" @click="operTask(item.Id, SYSTASKOPERTYPE.ReviewApproved)"
                        v-if="checkRight(item.State, SYSTASKOPERTYPE.ReviewApproved)">审核通过</uv-button>
                    <uv-button type="warning" size="mini" @click="operTask(item.Id, SYSTASKOPERTYPE.ReviewRejected)"
                        v-if="checkRight(item.State, SYSTASKOPERTYPE.ReviewRejected)">驳回</uv-button>
                </view>
            </view>

        </template>
    </z-paging>
</template>

<script lang="ts" setup>
import { GetSysTaskPage_API, SysTaskOperate_API } from '@/server/home'
import { useDictStore, useUserStore } from '@/stores'
import { SYSFORMOPERTYPE, SYSTASKOPERTYPE, TASKSTATECODE } from '@/utils/enum'
const dictStore = useDictStore()
let TownCode = ''
onLoad((e) => {
    TownCode = e.TownCode || ''
    dictStore.getDictData(['disaster_type', 'task_state'])
    //请求字典数据
    nextTick(() => {
        pagingRef.value.reload(true)
    })
})
onShow(() => {

})
const store = useUserStore()
const pagingRef = ref()
/**
 * 获取数据
 * @param pageNo 当前页
 * @param pageSize 条数
 */
const queryListFn = async (pageNo: any, pageSize: any) => {
    const params = {
        pageNumber: pageNo,
        pageSize: pageSize,
        // AdminCode: store.userInfo.AdminCode,
        AdminCode: TownCode
        // id: 14,
    }
    try {
        const res = await GetSysTaskPage_API(params)
        if (res.IsSuccess) {
            pagingRef.value.complete(res.Data.Data.map(el => ({
                ...el,
                StateText: dictStore.dictData['task_state'].find((item) => item.value == el.State)?.text || '',
                ObjectTypeCodeText: dictStore.dictData['disaster_type'].find((item) => item.value == el.ObjectTypeCode)?.text || '',
                StateType: stateType(el.State),
            })))
        }
    } catch (error) {
        pagingRef.value.complete(false)
    }
}
const stateType = (state) => {
    switch (state) {
        case TASKSTATECODE.PendingPublish:
            return 'primary';
        case TASKSTATECODE.Reviewed:
            return 'success';
        case TASKSTATECODE.PendingReview:
            return 'success';
        case TASKSTATECODE.Rejected:
            return 'error';
        case TASKSTATECODE.Expired:
            return 'error';
        case TASKSTATECODE.Deleted:
            return 'error';
        case TASKSTATECODE.PendingSubmission:
            return 'success';
        default:
            return 'primary';
    }
}
const useSotre = useUserStore()
const userType = useSotre.userInfo.UserType;
const checkRight = (state, optype) => {
    let right = false;
    switch (optype) {
        case SYSTASKOPERTYPE.Publish:
            right = userType == 0 && state == TASKSTATECODE.PendingPublish;
            break;
        case SYSTASKOPERTYPE.CancelPublish:
            right = userType == 0 && state == TASKSTATECODE.PendingSubmission;
            break;
        case SYSTASKOPERTYPE.ReviewApproved:
            right = userType == 0 && state == TASKSTATECODE.PendingReview;
            break;
        case SYSTASKOPERTYPE.ReviewRejected:
            right = userType == 0 && state == TASKSTATECODE.PendingReview;
            break;
        case SYSTASKOPERTYPE.Delete:
            right = userType == 0 && state == TASKSTATECODE.PendingPublish;
            break;
        case SYSTASKOPERTYPE.Submit:
            right = userType == 1 && state == TASKSTATECODE.PendingSubmission;
            break;
        case SYSTASKOPERTYPE.Withdraw:
            right = userType == 1 && state == TASKSTATECODE.PendingReview;
            break;
        case SYSTASKOPERTYPE.ReportView:
            right = state == TASKSTATECODE.Reviewed ||
                state == TASKSTATECODE.PendingReview ||
                state == TASKSTATECODE.Rejected;
            break;
    }
    return right;
}

const gotoTaskPage = (optype, id) => {
    switch (optype) {
        case SYSFORMOPERTYPE.View:
            uni.navigateTo({
                url: '/pages/home/xcxx-task-look?optype=' + SYSFORMOPERTYPE.View + '&id=' + id
            });
            break;
        case SYSFORMOPERTYPE.Add:

            uni.navigateTo({
                url: '/pages/home/xcxx-task?optype=' + SYSFORMOPERTYPE.Add
            });
            break;
        case SYSFORMOPERTYPE.Edit:
            uni.$off('refresh-data')
            uni.$once('refresh-data', () => {
                pagingRef.value.refresh()
            })
            uni.navigateTo({
                url: '/pages/home/xcxx-task?optype=' + SYSFORMOPERTYPE.Edit + '&id=' + id
            });
            break;
    }
}
const gotoReportPage = (optype, id) => {
    switch (optype) {
        case SYSFORMOPERTYPE.View:
            uni.navigateTo({
                url: '/pages/home/xcxx-report-look?optype=' + SYSFORMOPERTYPE.View + '&id=' + id
            });
            break;
        case SYSFORMOPERTYPE.Add:
            uni.navigateTo({
                url: '/pages/home/xcxx-report?optype=' + SYSFORMOPERTYPE.Add
            });
            break;
        case SYSFORMOPERTYPE.Edit:
            uni.navigateTo({
                url: '/pages/home/xcxx-report?optype=' + SYSFORMOPERTYPE.Edit + '&id=' + id
            });
            break;
    }
}
const operTask = async (id, optype) => {
    const res = await SysTaskOperate_API(optype, id)
    if (res.IsSuccess) {
        uni.$uv.toast('操作成功')
        pagingRef.value.refresh()
    }
}
</script>

<style lang="scss" scoped>
.tool {
    text-align: center;
    padding: 0rpx 40rpx;
}

.card-title-name {
    color: #000000;
    font-size: 32rpx;
    font-weight: bold;
    padding-left: 16rpx;
}

.card-title {
    color: #948b8b;
}

.card-content {
    font-size: 32rpx;
    color: #272727;
}
</style>
